package com.example.kuaizu.service.imp;

import com.example.kuaizu.job.dto.EveryProjectJoinContDTO;
import com.example.kuaizu.mapper.ProjectBaseMapper;
import com.example.kuaizu.service.InProjectBaseService;
import kuaizu.common.mqconst.MQConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author: wangmx15
 * @date: 2025/9/6
 **/
@Service
@Log4j2
@RequiredArgsConstructor
public class ProjectBaseService implements InProjectBaseService {
    //获取全部项目的投递人数
    private final ProjectBaseMapper projectBaseMapper;
    private final RabbitTemplate rabbitTemplate;

    @Override
    public void getEveryProjectJoinCont() {
        log.info("---获取全部项目的投递人数---");
        List<EveryProjectJoinContDTO> everyProjectJoinCont = projectBaseMapper.getEveryProjectJoinCont();
        Map<Integer, EveryProjectJoinContDTO> map = new HashMap<>();
        everyProjectJoinCont.stream().filter(everyProjectJoinContDTO -> (everyProjectJoinContDTO.getCount() > 3
                        || everyProjectJoinContDTO.getCount() < 3 && everyProjectJoinContDTO.getCount() > 0 &&
                        (null == everyProjectJoinContDTO.getReviewTime() ? everyProjectJoinContDTO.getCreateTime().plusDays(1).isBefore(LocalDateTime.now()) :
                        everyProjectJoinContDTO.getReviewTime().plusDays(1).isBefore(LocalDateTime.now()))))
                .forEach(everyProjectJoinContDTO -> {
                    map.put(everyProjectJoinContDTO.getProjectId(), everyProjectJoinContDTO);
                });
        if (CollectionUtils.isEmpty(map)) {
            log.info("没有需要发送的");
            return;
        }
        log.info("开始向mq发消息,数据:{}",map);
        map.forEach((projectId, everyProjectJoinContDTO) -> {
                rabbitTemplate.convertAndSend(MQConstant.PROJECT_CREATE_SUBSCRIBE_QUEUE, everyProjectJoinContDTO);
                log.info("mq发送邮件推广信息成功:{}", everyProjectJoinContDTO);
        });
    }
}
